<?php
namespace app\admin\controller;

use \think\Cache;
use \think\Controller;
use think\Loader;
use think\Db;
use \think\Cookie;
use app\admin\controller\Permissions;
use app\admin\model\Store as storeModel;
use app\admin\model\Emp as empModel;
use app\admin\model\Group as groupModel;
use app\admin\model\Storecate as addstoreModel;
use app\admin\model\Storelist as storelistModel;
use app\admin\model\Grouplist as grouplistModel;
class Store extends Permissions
{
	//门店设置
    public function index()
    {
   
    }
    
    //门店分类
    public function storelist()
    {
 	    $num = db('store')->group('storelist_id')->field('count(id) as num,storelist_id')->buildSql();
 	    $storelist =db('storelist')->alias('a')
        ->join("$num as b",'a.id=storelist_id','LEFT')
        ->field('a.*,b.num')->select();
        $this->assign('storelist',$storelist);
        return $this->fetch();
 	    
    }    
    //门店分类修改
	public function publishstorelist(){
    	$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    	if($id > 0) {
    		//是修改操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
				$res = db('storelist')->where('id',$id)->update($post);
                if($res==true){
                	return $this->success('修改成功','admin/store/storelist');
                }else{
                	return $this->error('修改失败');
                }
    		} else {
    			//非提交操作
    			$info['storelist'] = db('storelist')->where('id',$id)->find();
    			$this->assign('info',$info);
    			return $this->fetch();
    		}
    	} 
	}
    //新增门店分类
    public function addstorelist()
    {
    	$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    		//新增操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
	            //验证员工名是否存在
	            $name = db('storelist')->where('name',$post['name'])->select();
	            if(!empty($name)) {
	            	return $this->error('提交失败：该分类已被占用');
	        }
	            $res = db('storelist')->insert($post);
                if($res==true){
                	return $this->success('添加成功','admin/store/storelist');
                }else{
                	return $this->error('添加失败');
                }
    		}
    		else {
    			//非提交操作
    			$info['hyjt_storelist'] = Db::name('storelist')->select();
    			$this->assign('info',$info);
    			return $this->fetch();
    		}
		return $this->fetch();
	}
	//删除门店分类
    public function storelist_delete()
    {
    	if($this->request->isAjax()) {
    		$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    		$res = db('storelist')->where('id',$id)->delete();
    		if($res==true) {
    			return $this->success('删除成功','admin/store/storelist');
    		}else{
                return $this->error('删除失败');
    		}
    	}
    }
    
    //门店
	public function store(){
        $post = $this->request->param();
        if (isset($post['storelist_id']) and !empty($post['storelist_id'])) {
            $where['a.storelist_id'] = $post['storelist_id'];
        }
        if (isset($post['keywords']) and !empty($post['keywords'])) {
            $where['a.store_name'] = ['like', '%' . $post['keywords'] . '%'];
        }
		
        $store = empty($where) ? db('store')->alias('a')->join('storelist b','a.storelist_id=b.id','LEFT')->field('a.*,b.name')->order('a.show_asc asc')->paginate(20) : db('store')->alias('a')->join('storelist b','a.storelist_id=b.id','LEFT')->field('a.*,b.name')->where($where)->order('a.show_asc asc')->paginate(20,false,['query'=>$this->request->param()]);
        
        $this->assign('store',$store);
        $info['store'] = Db::name('store')->select();
        $this->assign('info',$info);
		return $this->fetch();
	}
	//门店修改
	public function publishstore()
    {
		$store = db('store')->where('id',input('id'))->find();
		$this->assign('store',$store);
		$where['store_name_id'] = input('id');
		$discount= db('storediscount')->where($where)->field('userlevel,userlevelname,discount,id')->select();
		$this->assign('discount',$discount);
		$storelist= Db::name('storelist')->select();
        $this->assign('storelist',$storelist);
		if(request()->isPost()){
			$result = true;
			Db::startTrans();
			 
			$data['store_id'] = $_POST['store_id'];  
			$data['storelist_id'] = $_POST['storelist_id'];
			$data['store_name'] = $_POST['store_name'];
			$data['store_principal'] = $_POST['store_principal'];
			$data['store_phonenumber'] = $_POST['store_phonenumber'];
			$data['store_address'] = $_POST['store_address'];
			if(!empty($_POST['thumb'])){
			$data['thumb'] = $_POST['thumb'];}
			$id = $_POST['id'];
			//不可以填写重复的门店名称
            $info1 = db('store')->where(['store_name'=>$_POST['store_name'],'id'=>['neq',$_POST['id']]])->find();
			if($info1){
				return $this->error('门店名称已存在，请重新填写');
			}
			$res = db('store')->where('id',$id)->update($data);
			
			if(!empty($_POST['discount'])){
			$ids = $_POST['ids'];  
			$discount = $_POST['discount'];
			//if(!$res) $result = false;
			$r=false;
			foreach($ids as $k=>$v){ 
	                $dis = $discount[$k];
					$data1['discount'] = $dis; 
			        if($v){
						$res1 = db('storediscount')->where('id',$v)->update($data1);
                        if($res1==true){$r=true;}
                          }
		    }
			if($res==false&&$r==false) $result = false;
		}
			 
			if($result){
				Db::commit();
				return $this->success('修改成功','admin/store/store'); 
			}else{
				Db::rollback();
				return $this->error('修改失败');
			}
          
		}
		return $this->fetch();
	}
    //新增门店
    public function addstore()
    {
    	$info['hyjt_store'] = Db::name('store')->select();
		$info['hyjt_storelist'] = Db::name('storelist')->select();
    	$info['hyjt_userlevel'] = Db::name('userlevel')->select();
        $this->assign('info',$info);
		if(request()->isPost()){
			$result = true;
			Db::startTrans();
			 
			$data['store_id'] = $_POST['store_id'];  
			$data['storelist_id'] = $_POST['storelist_id'];
			$data['store_name'] = $_POST['store_name'];
			$data['store_principal'] = $_POST['store_principal'];
			$data['store_phonenumber'] = $_POST['store_phonenumber'];
			$data['store_address'] = $_POST['store_address'];
			if(!empty($_POST['thumb'])){
			$data['thumb'] = $_POST['thumb'];}
			
			//$ids = $_POST['ids'];  
			//$userlevel = $_POST['userlevel']; 
			//$discount = $_POST['discount'];
 
            //不可以填写重复的门店名称
            $info1 = db('store')->where('store_name',$_POST['store_name'])->find();
			if($info1){
				return $this->error('该名称已添加，请重新填写');
			}

			$res = db('store')->insertGetId($data);
			if(!$res) $result = false;
			
			if(!empty($_POST['ids'])){
			$ids = $_POST['ids'];  
			$userlevel = $_POST['userlevel']; 
			$userlevelname = $_POST['userlevelname']; 
			$discount = $_POST['discount'];
			foreach($ids as $k=>$v){ 
	                $dis = $discount[$k];
	                $userl = $userlevel[$k];
	                $user2 = $userlevelname[$k];
					$data1['store_name_id'] = $res; 
					$data1['discount'] = $dis; 
					$data1['userlevel'] = $userl;
					$data1['userlevelname'] = $user2;
					//判断折扣有值的时候才记录表里
					//if(!empty($discount[$k])){
						$res1 = db('storediscount')->insert($data1);
					    if(!$res1) $result = false;
				//} 
	              }
	        }
	        
			
			if($result){
				Db::commit();
				return $this->success('添加成功','admin/store/store'); 
			}else{
				Db::rollback();
				return $this->error('添加失败');
			}
          
		}
		return $this->fetch();
	}
    //删除门店
     public function store_delete()
    {
		$res = db('store')->where('id',input('id'))->delete();
		if($res){
			db('storediscount')->where('store_name_id',input('id'))->delete();
		}
		if($res){
			return $this->success('删除成功','admin/store/store');
		}else{
			return $this->error('删除失败');
		}
  }    
    //分组
	public function group(){
        $post = $this->request->param();
        if (isset($post['store_name_id']) and !empty($post['store_name_id'])) {
            $where['a.store_name_id'] = $post['store_name_id'];
        }
        if (isset($post['keywords']) and !empty($post['keywords'])) {
            $where['a.principal|a.tel'] = ['like', '%' . $post['keywords'] . '%'];
        }
		
        $group = empty($where) ? db('group')->alias('a')
        ->join('store b','a.store_name_id=b.id','LEFT')
        ->field('a.*,b.store_name')
        ->order('a.show_asc asc')->paginate(20) : db('group')
        ->alias('a')->join('store b','a.store_name_id=b.id','LEFT')
        ->field('a.*,b.store_name')
        ->where($where)->order('a.show_asc asc')->paginate(20,false,['query'=>$this->request->param()]);
        
        $this->assign('group',$group);
        $info['hyjt_store'] = Db::name('store')->select();
        $this->assign('info',$info);
		return $this->fetch();
	}
	//分组修改
	public function publishgroup(){
    	$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    	if($id > 0) {
    		//是修改操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
				$res = db('group')->where('id',$id)->update($post);
                if($res==true){
                	return $this->success('修改成功','admin/store/group');
                }else{
                	return $this->error('修改失败');
                }
    		} else {
    			//非提交操作
    			$info['group'] = db('group')->where('id',$id)->find();
    			$info['hyjt_store'] = Db::name('store')->select();
    			$this->assign('info',$info);
    			return $this->fetch();
    		}
    	} 
	}
    //新增分组
    public function addgroup()
    {
    	$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    		//新增操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
	            //验证员工名是否存在
	            $name = db('group')->where('principal',$post['principal'])->select();
	            if(!empty($name)) {
	            	return $this->error('提交失败：该名称已被占用');
	        }
	            $res = db('group')->insert($post);
                if($res==true){
                	return $this->success('添加成功','admin/store/group');
                }else{
                	return $this->error('添加失败');
                }
    		}
    		else {
    			//非提交操作
    			$info['hyjt_store'] = Db::name('store')->select();
    			$info['hyjt_group'] = Db::name('group')->select();
    			$this->assign('info',$info);
    			return $this->fetch();
    		}
		return $this->fetch();
	}
    //删除分组
    public function group_delete()
    {
    	if($this->request->isAjax()) {
    		$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    		$res = db('group')->where('id',$id)->delete();
    		if($res==true) {
    			return $this->success('删除成功','admin/store/group');
    		}else{
                return $this->error('删除失败');
    		}
    	}
    }
    
    //员工
	public function emp()
	{
        $post = $this->request->param();
        if (isset($post['store_name_id']) and !empty($post['store_name_id'])) {
            $where['a.store_name_id'] = $post['store_name_id'];
        }
        if (isset($post['group_name_id']) and !empty($post['group_name_id'])) {
            $where['a.group_name_id'] = $post['group_name_id'];
        }
        if (isset($post['role_name_id']) and !empty($post['role_name_id'])) {
            $where['a.role_name_id'] = $post['role_name_id'];
        }
        if (isset($post['keywords']) and !empty($post['keywords'])) {
            $where['a.name|a.tel|c.group'] = ['like', '%' . $post['keywords'] . '%'];
        }
		
        $emp = empty($where) ? db('emp')->alias('a')
        ->join('store b','a.store_name_id=b.id','LEFT')
        ->join('group c','a.group_name_id=c.id','LEFT')
        ->join('admin_cate d','a.role_name_id=d.id','LEFT')
        ->field('a.*,b.store_name,c.group,d.name as role_name')
        ->order('a.show_asc asc')->paginate(20) : db('emp')
        ->alias('a')->join('store b','a.store_name_id=b.id','LEFT')
        ->join('group c','a.group_name_id=c.id','LEFT')
        ->join('admin_cate d','a.role_name_id=d.id','LEFT')
        ->field('a.*,b.store_name,c.group,d.name as role_name')
        ->where($where)->order('a.show_asc asc')->paginate(20,false,['query'=>$this->request->param()]);
        
        $this->assign('emp',$emp);
        $info['hyjt_store'] = Db::name('store')->select();
        $info['hyjt_group'] = Db::name('group')->select();
        $info['hyjt_admin_cate'] = Db::name('admin_cate')->where(['id'=>['neq',1]])->select();
        $this->assign('info',$info);
        return $this->fetch();
	}
	//员工修改
	public function publishemp()
	{
    	$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
		$this->assign('id',$id);
    	if($id > 0) {
    		//是修改操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
    			//设置业务员    			
    			$roleid = db('admin_cate')->where('name','业务员')->value('id');
    			if($roleid==$post['role_name_id']){
    			Db::name('user')->where('mobile',$post['tel'])->update(['is_ywy'=>1]);
    			}
    			//密码处理
    			if(!empty($post['password'])){
    				$post['password'] = md5_salt($post['password']);
    			}else{
    				unset($post['password']);
    			}
				if(!empty($post['oppass'])){
    				$post['oppass'] = md5_salt($post['oppass']);
    			}else{
    				unset($post['oppass']);
    			}
	            
				$res = db('emp')->where('id',$id)->update($post);
                if($res==true){
                	return $this->success('修改成功','admin/store/emp');
                }else{
                	return $this->error('修改失败');
                }
    		} else {
    			//非提交操作
    			$info['emp'] = db('emp')->where('id',$id)->find();
    			$info['hyjt_store'] = Db::name('store')->select();
    			$info['hyjt_group'] = Db::name('group')->where('store_name_id',$info['emp']['store_name_id'])->select();
                $info['hyjt_admin_cate'] = Db::name('admin_cate')->where(['id'=>['neq',1]])->select();
    			$this->assign('info',$info);
    			return $this->fetch();
    		}
    	} 
	}
    //新增员工
    public function addemp()
    {
    	$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    		//新增操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
    			//设置业务员
    			$roleid = db('admin_cate')->where('name','业务员')->value('id');
    			if($roleid==$post['role_name_id']){
    			Db::name('user')->where('mobile',$post['tel'])->update(['is_ywy'=>1]);
    			}
	            //验证员工名是否存在
	            $name = db('emp')->where('name',$post['name'])->select();
	            if(!empty($name)) {
	            	return $this->error('提交失败：该名称已被占用');
	        }
	        	//验证手机号是否被注册
	            $tel = db('emp')->where('tel',$post['tel'])->select();
	            if(!empty($tel)) {
	            	return $this->error('提交失败：该手机号已被注册');
	        }
	            //验证员工是否是会员
	            $member = db('user')->where('mobile',$post['tel'])->select();
	            if(empty($member)) {
	            	return $this->error('提交失败：请先注册为会员');

	        }
	            //绑定会员id
	        	$uid= db('user')->where('mobile',$post['tel'])->value('id');
	             if(!empty($uid)) {
	            	$post['user_id']=$uid;
	        }
	            //密码处理
	            $post['password'] = md5_salt($post['password']);
	            $post['oppass'] = md5_salt($post['oppass']);
	            $res = db('emp')->insert($post);
                if($res==true){
                	return $this->success('添加成功','admin/store/emp');
                }else{
                	return $this->error('添加失败');
                }
    		}
    		else {
    			//非提交操作
    			$info['hyjt_store'] = Db::name('store')->select();
    			//$info['hyjt_group'] = Db::name('group')->select();
    			//$info['hyjt_emp'] = Db::name('emp')->select();
    			$info['hyjt_admin_cate'] = Db::name('admin_cate')->where(['id'=>['neq',1]])->select();
    			$this->assign('info',$info);
    			return $this->fetch();
    		}
		return $this->fetch();
	}
    //删除员工
    public function emp_delete()
    {
    	if($this->request->isAjax()) {
    		$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    		$role_name_id = db('emp')->where('id',$id)->value('role_name_id');
    		$roleid = db('admin_cate')->where('name','业务员')->value('id');
    		$tel = db('emp')->where('id',$id)->value('tel');
    		if($roleid==$role_name_id){
    			Db::name('user')->where('mobile',$tel)->update(['is_ywy'=>0]);
    			$res = db('emp')->where('id',$id)->delete();
    			}
    		else{
    			$res = db('emp')->where('id',$id)->delete();
    		}
    			
            if($res==true) {
    			return $this->success('删除成功','admin/store/emp');
    		}else{
                return $this->error('删除失败');
    		}
    	}
    }
	
	/**
     * 获取门店下的分组
     */
	public function get_store()
	{
	    $store_name_id = input('store_name_id');
		$room = db('group')->where('store_name_id',$store_name_id)->field('group,id')->select();
		return json(["data"=>$room]);
	}
}
    
    
    
